package Stack;

import java.util.Stack;

public class _394_DecodeString {
    public String decodeString(String s) {
        String ans = "";
        String temp = "";
        Stack<String> stack = new Stack<>();
        stack.push("");
        for (char c : s.toCharArray()) {
//            System.out.println(stack.toString());
            if (c == '[') {
                if (temp != "") {
                    stack.push(temp);
                    temp = "";
                }
            } else if (c == ']') {
                if (temp != "") {
                    String prev = stack.pop();
                    String tempS = "";
                    if (Character.isDigit(prev.charAt(0))) {
                        int times = Integer.parseInt(prev);
                        for (int i = 0; i < times; i++) {
                            tempS += temp;
                        }
                    } else {
                        String preprev = stack.pop();
                        int times = Integer.parseInt(preprev);
                        for (int i = 0; i < times; i++) {
                            tempS += (prev + temp);
                        }
                    }
                    String bs = stack.peek();
                    if (bs != "") {
                        if (!Character.isDigit(bs.charAt(0))) {
                            stack.push(stack.pop() + tempS);
                        } else {
                            stack.push(tempS);
                        }
                    } else {
                        stack.push(tempS);
                    }
                    temp = "";
                } else {
                    String prev = stack.pop();
                    String preprev = stack.pop();
                    int times = Integer.parseInt(preprev);
                    String tempS = "";
                    for (int i = 0; i < times; i++) {
                        tempS += prev;
                    }
                    String bs = stack.peek();
                    if (bs != "") {
                        if (!Character.isDigit(bs.charAt(0))) {
                            stack.push(stack.pop() + tempS);
                        } else {
                            stack.push(tempS);
                        }
                    } else {
                        stack.push(tempS);
                    }
                }
            } else {
                if (Character.isLetter(c)) {
                    temp += c;
                } else {
                    if (temp != "" && !Character.isDigit(temp.charAt(0))) {
                        if (stack.peek()!="" && !Character.isDigit(stack.peek().charAt(0))) {
                            stack.push(stack.pop() + temp);
                        } else {
                            stack.push(temp);
                        }
                        temp = "";
                    }
                    temp += c;
                }
            }
        }
        while (!stack.isEmpty()) {
            ans += stack.pop();

        }
        if (temp != "") {
            ans += temp;
        }
        return ans;
    }
}
